select * from SaoJose2020..Venda where DOCTO  = 3432


update mercado..Venda set CODCLI = 1 where CODCLI = 0;

SELECT 
    DOCTO,
    COUNT(*) AS Qtde
FROM mercado..Venda
WHERE DOCTO IS NOT NULL
GROUP BY DOCTO
HAVING COUNT(*) > 1
ORDER BY Qtde DESC;


---migrar cabeçalho venda
set identity_insert BancoGerencial..VendaC on 
insert into BancoGerencial..VendaC
(Codigo,
CodCliente,
NomeCliente,
FormaPagamento,
TotalVenda,
DataMovimento,
DataVencimento, 
CodEmp,
SubTotal,
Desconto,
DataHora,
Usuario,
Status
)
select 
cli.DOCTO,
ltrim(rtrim(cli.CODCLI)),
'',
'',
ltrim(rtrim(cli.VALORTOT)),
ltrim(rtrim(cli.DTLANC)),
ltrim(rtrim(cli.DTLANC)),
1,
ltrim(rtrim(cli.PR_MERC)),
ltrim(rtrim(cli.PR_DESCONTO)),
ltrim(rtrim(cli.DTLANC)),
'MIGRAÇÃO',
'F'
from mercado..Venda as cli
where DTCANCEL is null and DOCTO is not null and Docto not in (853626,
653199,
808503,
769614,
772885,
807547,
966144,
912262,
565286,
929745,
660316,
822799,
994883,
822254,
812291,
897581,
848295,
887284,
482254,
848139,
510014,
958245,
872111,
779485,
883663,
672488,
949836,
849110)
and cli.CODCLI in (select codigo from cliente)
set identity_insert BancoGerencial..VendaC off

---acertar nome do cliente
update VendaC
set NomeCliente = n.Nome
from (select * from Cliente) as n
where n.Codigo = VendaC.CodCliente;

select * from VendaC

update vendaC set CodEmp = 1;
update vendaC set Terminal = 1;